package com.mia.props.events;

import com.mia.craftstudio.CSModel;
import com.mia.craftstudio.CraftStudioLib;
import com.mia.craftstudio.minecraft.CraftStudioRendererVBO;
import com.mia.craftstudio.minecraft.ModelMetadata;
import com.mia.craftstudio.minecraft.client.CSClientModelWrapperVBO;
import com.mia.craftstudio.minecraft.client.CSClientModelWrapperVariableVBO;
import com.mia.craftstudio.utils.ImageIOCS;
import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.client.fx.EntityCustomRainFX;
import com.mia.props.client.fx.ParticleHandler;
import com.mia.props.client.renderers.RenderPropInv;
import com.mia.props.client.renderers.RenderToolInv;
import com.mia.props.client.renderers.RendererProp;
import com.mia.props.common.TileProps;
import com.mia.props.network.NetworkHelper;
import cpw.mods.fml.client.registry.ClientRegistry;
import java.awt.image.BufferedImage;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.resources.IReloadableResourceManager;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.client.resources.IResourceManagerReloadListener;
import net.minecraft.command.ICommand;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.client.MinecraftForgeClient;
import org.lwjgl.opengl.GL11;

public class ClientProxy extends CommonProxy {
   public void setupModelWrappers() {
      ArrayList models = new ArrayList(Props.modelData.values());
      if (Props.itemTool.modelMeta != null) {
         models.add(Props.itemTool.modelMeta);
      }

      Iterator var2 = models.iterator();

      while(var2.hasNext()) {
         ModelMetadata modelData = (ModelMetadata)var2.next();
         if (modelData instanceof DecoModelMetadata && ((DecoModelMetadata)modelData).hasVariableRendering) {
            new CSClientModelWrapperVariableVBO(modelData);
         } else {
            new CSClientModelWrapperVBO(modelData);
         }

         Iterator var4 = modelData.csmodel.getTopNodes().iterator();

         while(var4.hasNext()) {
            CSModel.ModelNode node = (CSModel.ModelNode)var4.next();
            ((CSClientModelWrapperVBO)modelData.wrapper).addRenderer(new CraftStudioRendererVBO(modelData.wrapper.nodeCache.get(node)));
         }
      }

      ((IReloadableResourceManager)Minecraft.getMinecraft().getResourceManager()).registerReloadListener(new IResourceManagerReloadListener() {
         public void onResourceManagerReload(IResourceManager resourcemanager) {
            CraftStudioLib.getTimer().reset("Total mipmap");
            ArrayList models = new ArrayList(Props.modelData.values());
            if (Props.itemTool.modelMeta != null) {
               models.add(Props.itemTool.modelMeta);
            }

            Iterator var3 = models.iterator();

            while(var3.hasNext()) {
               ModelMetadata modelData = (ModelMetadata)var3.next();
               BufferedImage modelTexture = modelData.csmodel.getTexture();

               try {
                  modelTexture = ImageIOCS.read(Minecraft.getMinecraft().getResourceManager().getResource(new ResourceLocation("props".toLowerCase(), String.format("textures/models/%s", modelData.textureOverride))).getInputStream());
               } catch (Exception var9) {
               }

               ((CSClientModelWrapperVBO)modelData.wrapper).deleteGlTexture();
               ((CSClientModelWrapperVBO)modelData.wrapper).bindGlTexture();
               int mipmapLevels = Minecraft.getMinecraft().gameSettings.mipmapLevels;
               if (mipmapLevels > 0) {
                  GL11.glTexParameteri(3553, 33085, mipmapLevels);
                  GL11.glTexParameterf(3553, 33082, 0.0F);
                  GL11.glTexParameterf(3553, 33083, (float)mipmapLevels);
                  GL11.glTexParameterf(3553, 34049, 0.0F);
               }

               for(int lvl = 0; lvl <= mipmapLevels; ++lvl) {
                  GL11.glTexImage2D(3553, lvl, 6408, modelTexture.getWidth() >> lvl, modelTexture.getHeight() >> lvl, 0, 32993, 33639, (IntBuffer)null);
               }

               int[][] imgArray = new int[1 + mipmapLevels][];
               imgArray[0] = modelTexture.getRGB(0, 0, modelTexture.getWidth(), modelTexture.getHeight(), (int[])null, 0, modelTexture.getWidth());
               CraftStudioLib.getTimer().start("mipmap");
               int[][] mipdata = ImageIOCS.generateMipmapData(mipmapLevels, modelTexture.getWidth(), imgArray);
               CraftStudioLib.getTimer().stop("mipmap");
               CraftStudioLib.getTimer().add("Total mipmap", "mipmap");
               TextureUtil.uploadTextureMipmap(mipdata, modelTexture.getWidth(), modelTexture.getHeight(), 0, 0, false, false);
            }

            CraftStudioLib.getTimer().printAcc("Total mipmap");
         }
      });
   }

   public void init() {
      super.init();
      ClientEventHandler.register();
   }

   private void conditionallyRegisterCommand(String name) {
      try {
         ClientCommandHandler.instance.registerCommand((ICommand)ClientProxy.class.getClassLoader().loadClass(name).newInstance());
      } catch (ClassNotFoundException var3) {
      } catch (InstantiationException var4) {
      } catch (IllegalAccessException var5) {
      }

   }

   public void preInit() {
      ClientRegistry.bindTileEntitySpecialRenderer(TileProps.class, new RendererProp());
      MinecraftForgeClient.registerItemRenderer(Props.itemProps, new RenderPropInv());
      MinecraftForgeClient.registerItemRenderer(Props.itemTool, new RenderToolInv());
      this.conditionallyRegisterCommand("com.mia.props.commands.client.CommandExportToCS");
      this.conditionallyRegisterCommand("com.mia.props.commands.client.CommandMakeResources");
      NetworkHelper.INSTANCE.ordinal();
   }

   public void renderCustomRain(World world, double xp, double yp, double zp, float size) {
      ParticleHandler.INSTANCE.renderParticle(new EntityCustomRainFX(world, xp, yp, zp, size));
   }

   public TileEntity getTileEntity(int dim, int x, int y, int z) {
      return Minecraft.getMinecraft().theWorld.getTileEntity(x, y, z);
   }

   public void runningTEStateUpdate(int dim, int x, int y, int z, boolean state) {
      World world = Minecraft.getMinecraft().theWorld;
      if (world != null) {
         TileEntity entity = world.getTileEntity(x, y, z);
         if (entity instanceof TileProps) {
            ((TileProps)entity).setIsRunning(state);
         }

      }
   }
}
